package shuashuami.hb.com.fragment;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.adapter.CHallPlatformAdatper;
import shuashuami.hb.com.adapter.TaskAdatper;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpCMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.model.CPlatform;
import shuashuami.hb.com.model.Task;
import shuashuami.hb.com.util.CHallPlatformMenu;
import shuashuami.hb.com.util.LoadType;
import shuashuami.hb.com.view.pull.PullToRefreshLayout;

/**
 * <b>类   名：</b>${TYPE_NAME} <br/>
 * <b>类描述：</b> <br/>
 * <b>创建人：</b>zhangqi<br/>
 * <b>创建时间：</b>2017/6/13<br/>
 * <b>修改人：</b>zhangqi<br/>
 * <b>修改时间：</b>2017/6/13<br/>
 * <b>修改备注：</b><br/>
 *
 * @version 1.0.0<br/>
 */
public class HbCHallFragm extends AbFragment {

    private ListView listView;
    private TaskAdatper adatper;
    private List<Task> mResources;

    private LinearLayout llPlatform;
    private LinearLayout llChallPLatformMenu;
    private ListView llChallPLatformMenu2;

    private LinearLayout llPaixuform;
    private LinearLayout llChallPaixuMenu;
    private ListView llChallPaixuMenu2;

    private TextView tvPaixu, tvPingtai;

    private List<CPlatform> cPlatforms;
    CHallPlatformMenu cHallPlatformMenu;

    private List<CPlatform> cPaixu;
    CHallPlatformMenu cPaixuMenu;

    private int size = 10;
    private int maxpage = -1;
    private PullToRefreshLayout mptrl;
    private int page = 1;
    private int platform = 0;
    private String sort = "commission";
    private String is_vip = "0";

    @Override
    public void initView(View v) {
        setTitleBar("任务大厅");
        listView = (ListView) v.findViewById(R.id.lv_listview);
        mptrl = (PullToRefreshLayout) v.findViewById(R.id.refresh_view);
        llPlatform = (LinearLayout) v.findViewById(R.id.ll_chall_platform_menu);
        llChallPLatformMenu = (LinearLayout) v.findViewById(R.id.ll_chall_platform_menu_1);
        llChallPLatformMenu2 = (ListView) v.findViewById(R.id.ll_chall_platform_menu_2);

        llPaixuform = (LinearLayout) v.findViewById(R.id.ll_chall_paixu_menu);
        llChallPaixuMenu = (LinearLayout) v.findViewById(R.id.ll_chall_paixun_menu_1);
        llChallPaixuMenu2 = (ListView) v.findViewById(R.id.ll_chall_paixun_menu_2);
        tvPaixu = (TextView) v.findViewById(R.id.tv_chall_paixu_name);
        tvPingtai = (TextView) v.findViewById(R.id.tv_chall_pingtai_name);

    }

    @Override
    public void initData() {
        mResources = new ArrayList<>();
        adatper = new TaskAdatper(mResources, getActivity());
        listView.setAdapter(adatper);
        cPlatforms = new ArrayList<>();
        initPaixu();//排序方式
        initPaixuMenu();
        getChall();
        getChallPlatform();
    }

    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int what=msg.what;
            if(what==0x1){
                cHallPlatformMenu.closeMenu();


            }
            if(what==0x2){
                cPaixuMenu.closeMenu();
            }
        }
    };

    private void initPlatformMenu() {
        cHallPlatformMenu = new CHallPlatformMenu(llPlatform, llChallPLatformMenu, llChallPLatformMenu2, getActivity());
        cHallPlatformMenu.clickMenu();

        cHallPlatformMenu.demandWindow(mHandler,2);
    }

    private void initPaixuMenu() {
        cPaixuMenu = new CHallPlatformMenu(llPaixuform, llChallPaixuMenu, llChallPaixuMenu2, getActivity());
        cPaixuMenu.clickMenu();
        cPaixuMenu.demandWindow(mHandler,1);
    }

    public int operateType = LoadType.NO_OPERATE;

    @Override
    public void initListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent = new Intent(getActivity(), CHallInfoActivity.class);
//                startActivity(intent);
            }
        });

        llChallPLatformMenu2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                clickPlatformMenu(position);
            }
        });

        llChallPaixuMenu2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                clickPaixuMenu(position);
            }
        });
        mptrl.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {


            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                //
                operateType = LoadType.DO_REFERSH;
                page = 1;
                getChall();

            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                if (maxpage == -1 || page == maxpage) {
                    mptrl.loadmoreFinish(PullToRefreshLayout.NODATA);
                    return;
                }
//
                operateType = LoadType.LOAD_MORE;
                page++;
                getChall();
            }
        });
    }

    private void clickPlatformMenu(int position) {
        for (int i = 0; i < cPlatforms.size(); i++) {
            cPlatforms.get(i).setChoose(false);
        }
        cPlatforms.get(position).setChoose(true);
        cHallPlatformAdatper.notifyDataSetChanged();
        cPaixuMenu.closeMenu();
        tvPingtai.setText(cPlatforms.get(position).getName());
        mResources.clear();
        platform = Integer.valueOf(cPlatforms.get(position).getId());
        getChall();
    }

    private void clickPaixuMenu(int position) {
        for (int i = 0; i < cPaixu.size(); i++) {
            cPaixu.get(i).setChoose(false);
        }
        cPaixu.get(position).setChoose(true);
        cHallPlatformAdatper2.notifyDataSetChanged();
        cHallPlatformMenu.closeMenu();
        tvPaixu.setText(cPaixu.get(position).getName());
        mResources.clear();
        sort = cPaixu.get(position).getId();
        getChall();
    }

    private void initAdapter(List<Task> list) {


        // 如果是刷新
        if (operateType == LoadType.DO_REFERSH) {
            mResources.clear();
        }


        mResources.addAll(list);

        adatper.notifyDataSetChanged();
        if (operateType == LoadType.DO_REFERSH) {
            mptrl.refreshFinish(PullToRefreshLayout.SUCCEED);
        }
        if (operateType == LoadType.LOAD_MORE) {
            mptrl.loadmoreFinish(PullToRefreshLayout.SUCCEED);
        }

        operateType = LoadType.NO_OPERATE;

    }


    @Override
    public int bindView() {
        return R.layout.frag_chall;
    }

    public void getChallPlatform() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);
            }
        };
        HttpCMethods.getPlatform(listener);
    }

    private void getChall() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(2);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);
            }
        };
        HttpCMethods.getChallList(listener, page, size, platform + "", sort, is_vip);
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(getActivity());
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONArray jsonArray = jsonObject.getJSONArray("content");
                            CPlatform cPlatform0 = new CPlatform("0", "全部", "", true);

                            cPlatforms.add(cPlatform0);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject temp = jsonArray.getJSONObject(i);
                                CPlatform cPlatform;
                                if (i == 0) {
                                    cPlatform = new CPlatform(temp.getString("id"), temp.getString("name"), temp.getString("image"), false);
                                } else {
                                    cPlatform = new CPlatform(temp.getString("id"), temp.getString("name"), temp.getString("image"), false);
                                }
                                cPlatforms.add(cPlatform);
                            }
                            initPlatformAdapter();
                            initPlatformMenu();
                        } else {
                            ToastUtil.showShort(getActivity(), jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    try {
                        List<Task> list = new ArrayList<>();
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(getActivity());
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject jsonObject1 = jsonObject.getJSONObject("content");
                            page = jsonObject1.getInt("currentpage");
                            maxpage = jsonObject1.getInt("maxpage");
                            JSONArray jsonArray = jsonObject1.getJSONArray("list");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject temp = jsonArray.getJSONObject(i);
                                Task task;
                                if (temp.getInt("need_comment") == 2) {
                                    task = new Task(temp.getString("id"),
                                            temp.getString("platform_image"), temp.getString("addtime"), "需要", temp.getString("price"), temp.getString("commission"),
                                            temp.getString("task_sn"), temp.getInt("num"),
                                            temp.getString("type_name"), temp.getString("goods_type_name"), false, temp.getString("vip_commission"));
                                } else {
                                    task = new Task(temp.getString("id"),
                                            temp.getString("platform_image"), temp.getString("addtime"), "不需要", temp.getString("price"),
                                            temp.getString("commission"), temp.getString("task_sn"), temp.getInt("num"),
                                            temp.getString("type_name"), temp.getString("goods_type_name"), false, temp.getString("vip_commission"));
                                }
                                list.add(task);
                            }


                        } else {
                            ToastUtil.showShort(getActivity(), jsonObject.getString("message"));
                        }
                        initAdapter(list);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };
    CHallPlatformAdatper cHallPlatformAdatper;

    private void initPlatformAdapter() {
        cHallPlatformAdatper = new CHallPlatformAdatper(cPlatforms, getActivity());
        llChallPLatformMenu2.setAdapter(cHallPlatformAdatper);
        ;
    }

    CHallPlatformAdatper cHallPlatformAdatper2;

    private void initPaixu() {
        cPaixu = new ArrayList<>();
        CPlatform cPlatform;
        cPlatform = new CPlatform("commission", "默认排序", "", true);
        cPaixu.add(cPlatform);
        cPlatform = new CPlatform("commission", "佣金排序", "", false);
        cPaixu.add(cPlatform);
        cPlatform = new CPlatform("addtime", "时间排序", "", false);
        cPaixu.add(cPlatform);
        cHallPlatformAdatper2 = new CHallPlatformAdatper(cPaixu, getActivity());
        llChallPaixuMenu2.setAdapter(cHallPlatformAdatper2);


    }
}
